home *** CD-ROM | disk | FTP | other *** search
/ EuroCD 3 / EuroCD 3.iso / Utilities / Disk / DiskSafe / Extras / ResetTest.c < prev   
C/C++ Source or Header  |  1998-06-24  |  9KB  |  212 lines

  1. /*
  2.  * ResetTest.c
  3.  * (former Key_Reset.c)
  4.  *
  5.  * This is in two parts..
  6.  * here we've part one, part two is written in assembly and
  7.  * contains the reset-handler
  8.  * See: LSAVE:KeyHandler.a
  9.  *
  10.  * From: "AMIGA ROM Kernal Reference Manual" Part "Devices"
  11.  * typed in and modified by THOR in 12/26/91
  12.  *
  13.  * Additional note: YEAAAH ! works ! (never thought it will)
  14.  * 2. addit.  note: it is not save that this will work at your amiga, depends
  15.  *                  on your keyboard release
  16.  * 3. note:         Removed assembler stuff, now completely in C
  17.  *
  18.  */
  19.  
  20. /*******************************************
  21.  ** KeyBoard device reset handler example **
  22.  *******************************************/
  23.  
  24. #define INCLUDE_ALIB_PROTOS
  25.  
  26. #include <exec/types.h>
  27. #include <exec/io.h>
  28. #include <exec/ports.h>
  29. #include <exec/memory.h>
  30. #include <devices/keyboard.h>
  31. #include <intuition/intuition.h>
  32. #include <exec/interrupts.h>
  33.  
  34. #include <proto/exec.h>
  35. #include <proto/intuition.h>
  36. #define DOS_IO
  37. #include <proto/dos.h>
  38.  
  39. #include <stdio.h>
  40. #include <string.h>
  41.  
  42. #include <Startup.h>
  43.  
  44. struct IntuitionBase *IntuitionBase;
  45.  
  46. #ifdef LATTICE
  47. int CXBRK(void) { return(0); }          /* Disable SAS CTRL/C handling */
  48. int chkabort(void) { return(0); }       /* really */
  49. void main();
  50. #endif
  51.  
  52. struct MyData
  53.         {
  54.                 struct Task *MyTask;
  55.                 ULONG MySignal;
  56.         };
  57.  
  58. int __asm __saveds ResetHandler(register __a1 struct MyData *data);
  59.  
  60.  
  61.  
  62. UBYTE NameString[]="Reset Handler Test";
  63. struct NewWindow mywin={0,0,222,10,0,1,CLOSEWINDOW,
  64.                         WINDOWDRAG|WINDOWCLOSE|SIMPLE_REFRESH|NOCAREREFRESH,
  65.                         NULL,NULL,NameString,NULL,NULL,0,0,0,0,WBENCHSCREEN};
  66.                 
  67. extern struct IntuitionBase *IntuitionBase;
  68.  
  69.  
  70. /*
  71.  *
  72.  * This is the reset handler itself, here as SAS-C inline !
  73.  *
  74.  */
  75. int __asm __saveds ResetHandler(register __a1 struct MyData *data)
  76. {
  77.         Signal(data->MyTask,data->MySignal);
  78.  
  79.         return 0;
  80. }
  81.  
  82. /*
  83.  * This routine opens a window and waits for the one event that
  84.  * can happen (CLOSEWINDOW)
  85.  */
  86. short WaitForUser(ULONG MySignal)
  87. {
  88. struct Window *win;
  89. short ret=0;
  90.  
  91.         if (IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",0L))
  92.                 {
  93.                         if (win=OpenWindow(&mywin))
  94.                                 {
  95.                                         ret=(MySignal==Wait(MySignal | (1L<<win->UserPort->mp_SigBit)));
  96.                                         CloseWindow(win);
  97.                                 }
  98.                         else
  99.                                 printf("Error: Could not open window\n");
  100.                         CloseLibrary((struct Library *)IntuitionBase);
  101.                 }
  102.         else
  103.                 printf ("Error: Could not open intuition.library\n");
  104.         
  105.         return(ret);
  106. }
  107.  
  108. VOID main(int argc,char *argv[])
  109. {
  110. struct IOStdReq         *KeyIO;
  111. struct MsgPort          *KeyMP;
  112. struct Interrupt        *keyHandler;
  113. struct MyData           MyDataStuff;
  114. ULONG                   MySignal;
  115. int                     i;
  116.  
  117.         if ((MySignal=AllocSignal(-1L))!=-1) {
  118.                 MyDataStuff.MyTask=FindTask(NULL);
  119.                 MyDataStuff.MySignal=1L<<MySignal;
  120.                 
  121.                 if (KeyMP=CreatePort(NULL,NULL)) {
  122.                         if (keyHandler=AllocMem(sizeof(struct Interrupt),MEMF_PUBLIC|MEMF_CLEAR)) {
  123.                                 if (KeyIO=CreateStdIO(KeyMP)) {
  124.                                         if (!OpenDevice("keyboard.device",NULL,(struct IORequest *)KeyIO,NULL)) {
  125.                                                 keyHandler->is_Code=(APTR)&ResetHandler;
  126.                                                 keyHandler->is_Data=(APTR)&MyDataStuff;
  127.                                                 
  128.                                                 /*
  129.                                                  * Note that only software interrupt priorities
  130.                                                  * can be used for the .ln_Pri on the reset
  131.                                                  * handler, that means use ONLY
  132.                                                  * -32,-16,0,16 or 32...
  133.                                                  */
  134.                                                 keyHandler->is_Node.ln_Pri=16;
  135.                                                 
  136.                                                 keyHandler->is_Node.ln_Name=NameString;
  137.                                                 KeyIO->io_Data=(APTR)keyHandler;
  138.                                                 KeyIO->io_Command=KBD_ADDRESETHANDLER;
  139.                                                 DoIO((struct IORequest *)KeyIO);
  140.                                                 
  141.                                                 if (WaitForUser(MyDataStuff.MySignal)) {
  142.                                                         if (argc) /* Check if run from CLI */ {
  143.                                                                 printf("System going down\n");
  144.                                                                 printf("Cleaning up...\n");
  145.                                                                 /* Do a little countdown, just to have a nice show... */
  146.                                                                 for (i=10;i>=0;i--) {
  147.                                                                         printf("%d\n",i);
  148.                                                                         Delay(30L);
  149.                                                                 };
  150.                                                                 /*
  151.                                                                  * Please node that we've only
  152.                                                                  * 10 (TEN) seconds time to do our reset stuff
  153.                                                                  * After this period the keyboard will hard-reset the computer
  154.                                                                  * without any chance to stop it.
  155.                                                                  *
  156.                                                                  */
  157.                                                                 printf("*** POOF ***\n");
  158.                                                                 Delay(20L);
  159.  
  160.                                                         };
  161.                                                         /* We are done with our cleanup */
  162.                                                         
  163.                                                         KeyIO->io_Data=(APTR)keyHandler;
  164.                                                         KeyIO->io_Command=KBD_RESETHANDLERDONE;
  165.                                                         DoIO((struct IORequest *)KeyIO);
  166.                                                         
  167.                                                         /*
  168.                                                          * Note that since the above call
  169.                                                          * tells the system it is sage to reboot
  170.                                                          * and will cause the reboot if this
  171.                                                          * task was the last to say so, the call
  172.                                                          * never really returns... The system
  173.                                                          * just reboots...
  174.                                                          */
  175.                                                          
  176.                                                 }
  177.                                                 
  178.                                                 KeyIO->io_Data=(APTR)keyHandler;
  179.                                                 KeyIO->io_Command=KBD_REMRESETHANDLER;
  180.                                                 DoIO((struct IORequest *)KeyIO);
  181.                                                 
  182.                                                 CloseDevice((struct IORequest *)KeyIO);
  183.                                         }
  184.                                         else
  185.                                                 printf ("Error: Could not open keyboard.device\n");
  186.                                                 
  187.                                         DeleteStdIO(KeyIO);
  188.                                 }
  189.                                 else
  190.                                         printf("Error: Could not create I/O request\n");
  191.                                         
  192.                                 FreeMem(keyHandler,sizeof(struct Interrupt));
  193.                         }
  194.                         else
  195.                                 printf("Error: Could not allocate memory for interrupt\n");
  196.                                 
  197.                         DeletePort(KeyMP);
  198.                 }
  199.                 else
  200.                         printf("Error: Could not create message port\n");
  201.                         
  202.                 FreeSignal(MySignal);
  203.         }
  204.         else
  205.                 printf("Error: Could not allocate signal\n");
  206.                 
  207. }
  208.                                                         
  209.                                                 
  210.                                                 
  211.                                         
  212.